<?php
class Merchant_Service_OrderDeal
{
	public static function getParam($obj){
        $cn_description =$obj->getParam('cn_description',null);
        $hs_code =$obj->getParam('hs_code',null);
        $op_origin =$obj->getParam('op_origin',null);
        $op_quantity =$obj->getParam('op_quantity',null);
        $product_sku =$obj->getParam('product_sku',null);
        $op_description =$obj->getParam('op_description',null);
        $product_id =$obj->getParam('product_id',null);
        $op_id =$obj->getParam('op_id',array());

        $consignee_address1 =$obj->getParam('consignee_address1','');
        $consignee_address2 =$obj->getParam('consignee_address2','');
        $consignee_city =$obj->getParam('consignee_city','');
        $consignee_company =$obj->getParam('consignee_company','');
        $consignee_country_id =$obj->getParam('consignee_country_id','');
        $consignee_email =$obj->getParam('consignee_email','');
        $consignee_first_name =$obj->getParam('consignee_first_name','');
        $consignee_last_name =$obj->getParam('consignee_last_name','');
        $consignee_phone =$obj->getParam('consignee_phone','');
        $consignee_region =$obj->getParam('consignee_region','');
        $consignee_zip =$obj->getParam('consignee_zip','');
        $customer_order_code =$obj->getParam('customer_order_code','');
        $parcel_contents =$obj->getParam('parcel_contents','');
        $parcel_declared_value =$obj->getParam('parcel_declared_value','');
        $parcel_quantity =$obj->getParam('parcel_quantity','');
        $remark =$obj->getParam('remark','');
        $shipping_method =$obj->getParam('shipping_method','');
        $order_id =$obj->getParam('order_id',0);
		$shipping_fee_estimate =$obj->getParam('shipping_fee_estimate');

        $serviceObj = Merchant_Service_Product::getInstance();
        $loginInfo = $serviceObj->getLoginInfo();
        $customer_id = $loginInfo->customer["customer_id"];


        $order_product = array();
		if(is_array($product_sku)){
            foreach($product_sku as $i=>$val){
               $productObj = new Merchant_Service_Product();
               $product = $productObj->getProductInfo($product_id[$i]);
               $order_product[] =array(
                   'op_id' => $op_id[$i],
                   'product_id' => $product_id[$i],
                   //'cn_description' => $val,
                   //'hs_code' => $hs_code[$i],
                   //'op_origin' => $op_origin[$i],
                   'op_quantity' => $op_quantity[$i],
                   'product_sku' => $product_sku[$i],
                   'op_description' => $op_description[$i],
                   'op_unit_price' => $product['product_declared_value'],
                   'op_subtotal' => round($product['product_declared_value']*$op_quantity[$i],2),
                   'op_declared_value' => round($product['product_declared_value']*$op_quantity[$i],2),
                   'op_weight' => round($product['product_weight']*$op_quantity[$i],2),
               );
            }
		}


        $orderInfo =array(
            'order_id' =>$order_id,
            'customer_id' =>$customer_id,
            'consignee_address1' => $consignee_address1,
            'consignee_address2' =>$consignee_address2,
            'consignee_city' =>$consignee_city,
            'consignee_company' =>$consignee_company,
            'consignee_country_id' =>$consignee_country_id,
            'consignee_email' =>$consignee_email,
            'consignee_first_name' =>$consignee_first_name,
            'consignee_last_name' =>$consignee_last_name,
            'consignee_phone' =>$consignee_phone,
            'consignee_region' =>$consignee_region,
            'consignee_zip' => $consignee_zip,
            'customer_order_code' => $customer_order_code,
            'parcel_contents' => $parcel_contents,
            'parcel_declared_value' => $parcel_declared_value,
            'parcel_quantity' => $parcel_quantity,
            'remark' => $remark,
            'shipping_method' => $shipping_method,
            'order_product' =>$order_product,
            'shipping_fee_estimate' =>$shipping_fee_estimate,
        );
        return  $orderInfo;
	}


    public static function edit($orderInfo){
        $retrun =array();
        $return['ask'] =false;
        $return['list']['message'] = "操作失败";

        $order = array(
            'customer_id' => $orderInfo['customer_id'],
            'consignee_address1' => $orderInfo['consignee_address1'],
            'consignee_address2' => $orderInfo['consignee_address2'],
            'consignee_city' => $orderInfo['consignee_city'],
            'consignee_company' => $orderInfo['consignee_company'],
            'consignee_country_id' => $orderInfo['consignee_country_id'],
            'consignee_email' => $orderInfo['consignee_email'],
            'consignee_first_name' => $orderInfo['consignee_first_name'],
            'consignee_last_name' => $orderInfo['consignee_last_name'],
            'consignee_phone' => $orderInfo['consignee_phone'],
            'consignee_region' => $orderInfo['consignee_region'],
            'consignee_zip' => $orderInfo['consignee_zip'],
            'customer_order_code' => $orderInfo['customer_order_code'],
            'parcel_contents' => $orderInfo['parcel_contents'],
            'parcel_declared_value' => $orderInfo['parcel_declared_value'],
            'parcel_quantity' => $orderInfo['parcel_quantity'],
            'remark' => $orderInfo['remark'],
            'shipping_method' => $orderInfo['shipping_method'],
            'shipping_fee_estimate' => $orderInfo['shipping_fee_estimate'],
        );

        if($orderInfo['order_id']>0)
        {
            $order_id = $orderInfo['order_id'];
            Merchant_Service_Orders::update($order,$order_id);
            $order_code= Merchant_Service_Orders::getByValue($order_id,'order_id');
            $condition =array('orders_code'=>$order_code['orders_code']);
            $orderProductSelect= Merchant_Service_OrderProduct::getByCondition($condition,'op_id');
            $opIdArr =array();
            foreach($orderProductSelect as $row){
                $opIdArr[] = $row['op_id'];
            }
            foreach($orderInfo['order_product'] as $order_product){
                $orderProductInfo = array(
                   'orders_code' => $order_code['orders_code'],
                   'product_id' => $order_product['product_id'],
                   //'cn_description' => $order_product['cn_description'],
                   //'hs_code' => $order_product['hs_code'],
                   //'op_origin' => $order_product['op_origin'],
                   'op_quantity' => $order_product['op_quantity'],
                   'product_sku' => $order_product['product_sku'],
                   'op_description' => $order_product['op_description'],
                   'op_unit_price' => $order_product['op_unit_price'],
                   'op_subtotal' => $order_product['op_unit_price'],
                   'op_declared_value' => $order_product['op_declared_value'],
                   'op_weight' => $order_product['op_weight'],
                );
                if(in_array($order_product['op_id'],$opIdArr)){
                    unset($opIdArr[$order_product['op_id']]);
                    Merchant_Service_OrderProduct::update($orderProductInfo,$order_product['op_id']);
                }else{
                    Merchant_Service_OrderProduct::add($orderProductInfo);
                }
                foreach($opIdArr as $op_id){
                    Merchant_Service_OrderProduct::delete($op_id);
                }
            }
            $return['ask'] =true;
			$return['ordersCode'] = $order_code['orders_code'];
            $return['list']['message'] = "订单:{$order_code['orders_code']}修改成功";
        }else{

            $order_code = Merchant_Service_OrderId::getOrderId('W',$orderInfo['customer_id']);
            $order['orders_status'] = 1;
            $order['orders_code'] = $order_code;
            $order['add_time'] = date('Y-m-d H:i:s');
			$model = new Merchant_Model_Orders();
			$db = $model->getAdapter();
			$db->beginTransaction();
			try{				 
				if(!Merchant_Service_Orders::add($order)){
						$db->rollback();
						$return['list']['message'] = 'Internal Error!';	
				}
				foreach($orderInfo['order_product'] as $order_product){
					$orderProductInfo = array(
					   'orders_code' => $order_code,
					   'product_id' => $order_product['product_id'],
					   //'cn_description' => $order_product['cn_description'],
					   //'hs_code' => $order_product['hs_code'],
					   //'op_origin' => $order_product['op_origin'],
					   'op_quantity' => $order_product['op_quantity'],
					   'product_sku' => $order_product['product_sku'],
					   'op_description' => $order_product['op_description'],
					   'op_unit_price' => $order_product['op_unit_price'],
					   'op_subtotal' => $order_product['op_unit_price'],
					   'op_declared_value' => $order_product['op_declared_value'],
					   'op_weight' => $order_product['op_weight'],
					);
					if(!Merchant_Service_OrderProduct::add($orderProductInfo)){
						$db->rollback();
						$return['list']['message'] = 'Internal Error!';	
					}
				}
				$db->commit();
				$return['ask'] =true;
				$return['ordersCode'] = $order_code;
				$return['list']['message'] = "订单:{$order_code}添加成功";
			}catch(Exception $e){
				$db->rollback();
				$return['list']['message'] = 'Internal Error!';	
			}
           
        }
        return $return;
    }
}